package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.other.service.dto.PrivacyAgreementDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: youqing.yang
 * @create: 2020/10/14 10:18
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：隐私协议")
@RequestMapping("/api/privacyAgreement")
public class PrivacyAgreementController {

    @Autowired
    private CorporationService corporationService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation("集团隐私协议-展示")
    @GetMapping
    public ResponseEntity<PrivacyAgreementDto> list(){
        Corporation corporation = corporationService.getCorporationByCorporationCode(SecurityUtils.getCorporationCode());
        if (corporation == null) {
            throw new BusinessException("未找到对应的集团信息");
        }
        PrivacyAgreementDto privacyAgreementDto = new PrivacyAgreementDto();
        privacyAgreementDto.setPrivacyAgreement(corporation.getPrivacyAgreement());
        return ResponseEntity.ok(privacyAgreementDto);
    }

    @ApiOperation("集团隐私协议—修改")
    @PutMapping
    public ResponseEntity<Object> updatePrivacyAgreement(@RequestBody PrivacyAgreementDto privacyAgreementDto){
        Corporation corporation = corporationService.getCorporationByCorporationCode(SecurityUtils.getCorporationCode());
        if (corporation != null && StringUtils.isNotEmpty(corporation.getId())) {
            Criteria criteria = Criteria.where("id").is(corporation.getId());
            Update update = new Update();
            update.set("privacyAgreement",privacyAgreementDto.getPrivacyAgreement());
            update.set("updateTime", new Date());
            mongoTemplate.updateFirst(new Query(criteria), update, Corporation.class);
        }
        return ResponseEntity.ok(HttpStatus.OK);
    }

    @ApiOperation("集团官网隐私协议-展示")
    @GetMapping("/getPcPrivacyAgreement")
    public ResponseEntity<PrivacyAgreementDto> getPcPrivacyAgreement(){
        Corporation corporation = corporationService.getCorporationByCorporationCode(SecurityUtils.getCorporationCode());
        if (corporation == null) {
            throw new BusinessException("未找到对应的集团信息");
        }
        PrivacyAgreementDto privacyAgreementDto = new PrivacyAgreementDto();
        privacyAgreementDto.setPrivacyAgreement(corporation.getPCprivacyAgreement());
        return ResponseEntity.ok(privacyAgreementDto);
    }

    @ApiOperation("集团官网隐私协议—修改")
    @PutMapping("/updatePcPrivacyAgreement")
    public ResponseEntity<Object> updatePcPrivacyAgreement(@RequestBody PrivacyAgreementDto privacyAgreementDto){
        Corporation corporation = corporationService.getCorporationByCorporationCode(SecurityUtils.getCorporationCode());
        if (corporation != null && StringUtils.isNotEmpty(corporation.getId())) {
            Criteria criteria = Criteria.where("id").is(corporation.getId());
            Update update = new Update();
            update.set("PCprivacyAgreement",privacyAgreementDto.getPrivacyAgreement());
            update.set("updateTime", new Date());
            mongoTemplate.updateFirst(new Query(criteria), update, Corporation.class);
        }
        return ResponseEntity.ok(HttpStatus.OK);
    }




}
